import StatusNav from '@/components/school/statusNav';
import PageBody from '@/components/school/pageBody';
import {mapGetters} from "vuex";
import * as TaskInfoApi from '@/api/school/taskInfo.js';
import diyJson from '@/config/diy.js';

const app = getApp();

export const mixins =
    {
      computed: mapGetters(['uid', 'userInfo', 'shareInfo', 'schoolId']),
      components: {
        StatusNav,
        PageBody,
      },
      data() {
        return {
          statusNavProperties: { // 筛选条件
            title: "跑腿服务详情",
            navStyle:{
              // position:'fixed',
              // left:0,
              // top:0,
              // 'z-index':100000
            },
            btnStyle:{
              backgroundColor: ''
            },
            showLeft: true,
            showHome: true,
            setStyle: {
              bgColor: "white",
              titleColor: "",
              titleStyle: {}
            }
          },

          swiperCurrent: 0,

          btnDisabled: false,

          themConfig: diyJson.commonStyle,

          pageBody: {
            paddingBottom: 65,
          },
          taskInfoGoods: {
            mainPic: []
          },
          form: {
            skuId: '',
            num: 1,
            salesPrice: 3,
            skuName: ''
          },
          urlParams: undefined,
        };
      },

      onLoad: function (options) {
        let p = options.params;
        if (p) {
          this.urlParams = decodeURIComponent(options.params);
          console.log("urlParams",this.urlParams)
          let params = JSON.parse(this.urlParams);
          this.getTaskDetail(params.taskCode, this.schoolId);
        }
      },

      created() {
      },

      onShow() {

      },
      mounted() {
        // this.$refs.formPopup.open();
      },

      methods: {
        selectSku(sku) {
          this.form.skuId = sku.skuId;
          this.form.salesPrice = sku.salesPrice;
          this.form.skuName = sku.name;
          console.log("sku", sku)
        },
        async getTaskDetail(taskCode, schoolId) {
          await TaskInfoApi.getTaskByCode(taskCode, schoolId).then(res => {
            this.taskInfoGoods = res.data
            this.form.salesPrice = res.data.salePrice;
            this.form.errandChargeAmount = res.data.errandChargeAmount;
            this.form.taskId = res.data.id;
            if (res.data.skus && res.data.skus.length === 1) {
              this.form.skuId = res.data.skus[0].skuId
              this.form.salesPrice = res.data.skus[0].salesPrice;
            }
          });
        },

        closeFormPopup() {
          // this.$refs.formPopup.close();
          let params = JSON.parse(this.urlParams);
          delete params['taskId'];
          let urlQuery = {
            ...params,
            ...this.form
          }
          uni.navigateTo({
            url: `/pages/school/buy-model/submit/index?params=${JSON.stringify(
                urlQuery)}`
          })
        },

        // 提交表单
        submit() {
          if (this.taskInfoGoods.skus) {
            if (this.taskInfoGoods.skus.length > 1) {
              this.form.skuName = this.taskInfoGoods.skus[0].name;
              this.form.skuId = this.taskInfoGoods.skus[0].skuId
              this.form.salesPrice = this.taskInfoGoods.skus[0].salesPrice;
              this.$refs.formPopup.open();
              return;
            }
            this.form.salesPrice = this.taskInfoGoods.skus[0].salesPrice;
            this.form.skuId = this.taskInfoGoods.skus[0].skuId
          }
          let params = JSON.parse(this.urlParams);
          let urlQuery = {
            ...params,
            ...this.form
          }
          uni.navigateTo({
            url: `/pages/school/buy-model/submit/index?params=${encodeURIComponent(JSON.stringify(
                urlQuery))}`
          })
        },

        getApp() {
          return app;
        },
      },
      onReachBottom() {

      }
    }
